package com.carlinfo.back.servlet;

import java.io.IOException;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.carlinfo.common.util.DateUtil;
import com.carlinfo.common.util.PageInfoUtil;
import com.carlinfo.demo.pojo.ADynasty;
import com.carlinfo.demo.service.IDemoService;
import com.carlinfo.demo.service.impl.DemoServiceImpl;

/**
 * 一个模块对应一个service 朝代还有皇上:CRUD
 */
public class DemoBackServlet extends HttpServlet
{
	private Logger logger = LogManager.getLogger() ; 
	private DateUtil dateUtil = new DateUtil() ; 
	
	/* 创建一个demoService */
	private IDemoService demoService = new DemoServiceImpl();
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 此代码一定要写到第一行
		 * 但是这种方式只能处理post请求
		 *  */
		request.setCharacterEncoding("UTF-8");
		this.logger.info("--DemoBackServlet--service--");
		/* ?mlq=demoList */
		String method = request.getParameter("method");
		/* 如果是老版本的浏览器,处理乱码最万能的是 */
		if("get".equalsIgnoreCase(request.getMethod()))
		{
			/* 解码:把字符串变成字节数组 */
			byte[] b = method.getBytes("ISO8859-1");
			/* 字节数组变成字符串
			 * 编码不一样
			 *  */
			method = new String(b,"UTF-8");
		}
		this.logger.info("--DemoBackServlet--method:{}--",method);
		if("dynastyList".equalsIgnoreCase(method))
		{
			/* 查询多条朝代
			 * method的值,方法,jsp的名字,三个是一样的
			 *  */
			this.dynastyList(request,response);
		}else if("dynastyInsert".equalsIgnoreCase(method))
		{
			/* 打开朝代添加的页面
			 *  */
			this.dynastyInsert(request,response);
		}else if("dynastyInsertSubmit".equalsIgnoreCase(method))
		{
			/* 朝代添加提交的页面
			 *  */
			this.dynastyInsertSubmit(request,response);
		}
	}
	
	/**
	 * 朝代添加提交
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void dynastyInsertSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		this.logger.info("--DemoBackServlet--dynastyInsertSubmit--");
		/* 从jsp中获取 */
		String name = request.getParameter("name");
		String capital = request.getParameter("capital");
		String age = request.getParameter("age");
		String st = request.getParameter("st");
		String pubTime = request.getParameter("pubTime");
		String context = request.getParameter("context");
		String status = request.getParameter("status");
		
		/* new出来的,属性有值? */
		ADynasty dynasty = new ADynasty() ; 
		dynasty.setName(name);
		dynasty.setCapital(capital);
		/* 420--->266: */
		dynasty.setAge(Integer.valueOf(age));
		dynasty.setContext(context);
		/* dateUtil */
		dynasty.setSt(this.dateUtil.strToDateTime(st));
		dynasty.setPubTime(this.dateUtil.strToDateTime(pubTime));
		
		dynasty.setStatus(Byte.valueOf(status));
		dynasty.setCreateTime(new Date());
		dynasty.setUpdateTime(new Date());
		
		/* 保存一条记录 */
		JSONObject resultJSON = this.demoService.saveOneDynastyService(dynasty);
		this.logger.info("返回值:{}",resultJSON);
		
		/* 往request中放的是json */
		request.setAttribute("resultJSON", resultJSON);
		/* 保存成功,又跳转到朝代添加的jsp中 */
		this.dynastyInsert(request, response);
	}

	/**
	 * 打开朝代添加的页面
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void dynastyInsert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		this.logger.info("--DemoBackServlet--dynastyInsert--");
		/* 服务器端跳转:
		 * 为啥要跳转:因为写html回信,jsp善长 */
		String path = "/WEB-INF/jsp/back/demo/dynastyInsert.jsp" ; 
		request.getRequestDispatcher(path).forward(request, response);
	}

	/**
	 * 查询多条朝代
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void dynastyList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		this.logger.info("--DemoBackServlet--dynastyList--");
		/* --实现搜索
		 * 搜索的所有条件由原来的死值写活
		 * 就是将搜索的条件问jsp要;
		 * jsp-->servlet--service-->dao-->db
		 * 
		 * ?a=1&b=2&c=3&d=
		 * 返回值是字符串;
		 * 	信里面压根就木有此键和值;request.getParameter("e");返回值就是null;
		 * 	信里面有此键和值,但是值是空的;空字符串;d是空字符串
		 * 	信里面有此键和值,正常情况;像a,b,c
		 * 
		 * 三种情况想办法合成一种情况;
		 * --- */
		String keyword = request.getParameter("keyword");
		String status = request.getParameter("status");
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");
		
		/* 处理参数 */
		if(keyword == null)
		{
			keyword = "" ; 
		}
		if(status == null)
		{
			status = "" ; 
		}
		if(st == null)
		{
			st = "" ; 
		}
		if(ed == null)
		{
			ed = "" ; 
		}
		
		/* 为了实现分页;
		 * 所需要的数据写活,问发信人的信里面要
		 * 取信的内容
		 * ?a=1&b=2&c=3
		 *  */
		String currentPage = request.getParameter("currentPage");
		String pageSize = request.getParameter("pageSize");
		
		/* 查询10条 */
		PageInfoUtil pageInfoUtil = new PageInfoUtil() ; 
		try
		{
			/* 如果currentPage发信人木有传,就是null;知道会报,
			 * try一下,异常不打印
			 *  */
			/* 每页10条, */
			pageInfoUtil.setPageSize(Integer.valueOf(pageSize));
			/* 第一页 */
			pageInfoUtil.setCurrentPage(Integer.valueOf(currentPage));
		} catch (NumberFormatException e)
		{
		}
		
		/* 存储的是查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 按照关键字搜索 */
		condMap.put("keyword", keyword);
		/* 按照状态搜索 */
		condMap.put("status", status);
		/* 按照时间范围搜索
		 * stDate:开始时间
		 * edDate:结束时间
			request.getParameter返回值是字符串;
			但是时间搜索我们要的是时间类型;
			将字符串变成时间类型
		 */
		Date edDate = null ; 
		Date stDate = null ;
		if(!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			stDate = this.dateUtil.strToDateTime(st) ;
			edDate = this.dateUtil.strToDateTime(ed) ;
		}
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		//this.logger.info("{}----->{}",stDate.toLocaleString(),edDate.toLocaleString());
		
		/* 排序:必须是指定值 */
		//condMap.put("orderBy", "idAscNameDesc");
		
		List<ADynasty> dynastyList = this.demoService.selectListDynastyService(pageInfoUtil,condMap);
		/* 站在使用者的角度,不想分页,直接将pageInfoUtil传成null */
		//List<ADynasty> dynastyList = this.demoService.selectListService(null,condMap);
		/* 所有在测试类中打印的代码统统放到jsp上面
		 * 数据的产生是在demoBackServlet中
		 * 数据的显示是在dynastyList中
		 * 他们两个可是不同的Servlet;如何将数据传过去呢?
		 * 四大作用域:page < request < session < application
		 * 原则:能用小的,别用大的;
		 * 
		 * 在任何地方,取数据的时候,一定要想三遍:我放了木有,我放了木有,我放了木有;
		 *  */
		request.setAttribute("dynastyList", dynastyList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		/* 将所有的搜索条件存储 */
		request.setAttribute("keyword", keyword);
		request.setAttribute("status", status);
		request.setAttribute("st", st);
		request.setAttribute("ed", ed);
		
		/* 服务器端跳转:
		 * 为啥要跳转:因为写html回信,jsp善长 */
		String path = "/WEB-INF/jsp/back/demo/dynastyList.jsp" ; 
		request.getRequestDispatcher(path).forward(request, response);
	}
}
